package com.liukai.novel_type.fragment;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.liukai.novel_base.base.BaseFragment;
import com.liukai.novel_base.base.GridAutofitLayoutManager;
import com.liukai.novel_base.base.GridSpacesItemDecoration;
import com.liukai.novel_commons.Result;
import com.liukai.novel_commons.entities.NovelInfo;
import com.liukai.novel_commons.entities.NovelType;
import com.liukai.novel_commons.utils.DpUtils;
import com.liukai.novel_commons.utils.JSONUtil;
import com.liukai.novel_commons.utils.LogUtils;
import com.liukai.novel_commons.utils.State;
import com.liukai.novel_info.ui.NovelDescActivity;
import com.liukai.novel_rack.search_interface.OnSearchItemListener;
import com.liukai.novel_rack.ui.SearchResultActivity;
import com.liukai.novel_type.R;
import com.liukai.novel_type.R2;
import com.liukai.novel_type.adapter.TypePagerRecyclerAdapter;
import com.liukai.novel_type.call_back.ITypePagerCallBack;
import com.liukai.novel_type.presenter.impl.TypePagerPresenterImpl;

import java.util.List;

import butterknife.BindView;

public class TypePagerFragment extends BaseFragment implements ITypePagerCallBack {

    // 小说分类信息
    private NovelType mNovelType;
    private TypePagerPresenterImpl mTypePagerPresenter;

    @BindView(R2.id.textView)
    TextView textView;
    @BindView(R2.id.typePagerRecyclerView)
    RecyclerView mRecyclerView;
    private TypePagerRecyclerAdapter mTypePagerRecyclerAdapter;
    private GridLayoutManager mGridLayoutManager;
    private Context mContext;

    public TypePagerFragment(NovelType novelType) {
        super();
        this.mNovelType = novelType;
    }

    /**
     * 加载数据
     * @param view
     */
    @Override
    protected void initDate(View view) {
        super.initDate(view);
        setUpState(State.LOADING);
        mTypePagerPresenter = new TypePagerPresenterImpl();
        mTypePagerPresenter.registerCallback(this);
        mTypePagerPresenter.getNovelByType(mNovelType.getTypeId());
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        textView.setText(mNovelType.getTypeName());
    }

    @Override
    protected void initAdapter(View view) {
        super.initAdapter(view);
        mTypePagerRecyclerAdapter = new TypePagerRecyclerAdapter();
//        mGridLayoutManager = new GridLayoutManager(getContext(),3);
        GridAutofitLayoutManager layoutManager = new GridAutofitLayoutManager(getContext(), DpUtils.dip2px(getContext(),80));
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.addItemDecoration(new GridSpacesItemDecoration( DpUtils.dip2px(getContext(),10),true));
        mRecyclerView.setAdapter(mTypePagerRecyclerAdapter);

        // 小说点击事件
        mTypePagerRecyclerAdapter.setOnSearchItemListener(new OnSearchItemListener() {
            @Override
            public void onItemClick(NovelInfo novelInfo) {
                Intent intent = new Intent(mContext, NovelDescActivity.class);
                intent.putExtra("novelInfo",novelInfo);
                startActivity(intent);
            }
        });
    }

    @Override
    protected Context getFragmentContext(View view) {
        mContext = view.getContext();
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_type_pager;
    }

    /**
     * 返回根据小说分类获取的小说
     * @param result
     */
    @Override
    public void setNovelByType(Result result) {
        // 解析数据
        List<NovelInfo> novelInfoList = JSONUtil.resolverResultList(result, NovelInfo.class);
        // 网络错误
        if (result.getCode() == 404){
            setUpState(State.ERROR);
            return;
        }
        if (novelInfoList.size() == 0 || novelInfoList == null){
            setUpState(State.EMPTY);
            return;
        }
        setUpState(State.SUCCESS);
        mTypePagerRecyclerAdapter.setNovelInfoList(novelInfoList);

    }

}
